/**
 * 
 * @author Marina Polese
 *
 */

package capture;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jpcap.packet.Packet;
import options.view.ViewSettings;
import capture.analyzer.AbstractPacketAnalyzer;

public class CapturePanel extends JPanel implements ListSelectionListener {

	private static final long serialVersionUID = 497158696253896552L;
	private TablePacket table;
	private PacketProtocolTree tree;
	private PacketTableTextArea text;
	private Captor captor; 
	private List<AbstractPacketAnalyzer> analyzers;

	public CapturePanel(Captor captor){
		ViewSettings.getSettings().setLookAndFeel(this);

		this.captor = captor;
		analyzers = captor.getFrame().getAnalyzers();
		table = new TablePacket(this,captor);
		tree = new PacketProtocolTree();
		text = new PacketTableTextArea();

		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane splitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setTopComponent(table);
		splitPane2.setTopComponent(tree);
		splitPane2.setBottomComponent(new JScrollPane(text));
		splitPane.setBottomComponent(splitPane2);
		splitPane.setDividerLocation(200);
		splitPane2.setDividerLocation(200);
		setLayout(new BorderLayout());
		add(splitPane,BorderLayout.CENTER);

		loadProperty();
		setSize(400,200);
	}

	public void fireTableChanged(){
		table.fireTableChanged();
	}

	public void clearTable(){
		table.clear();
	}

	public void clearTextArea(){
		text.clear();
	}

	public void clearTree(){
		tree.clear();
	}

	public void valueChanged(ListSelectionEvent evt){
		if(evt.getValueIsAdjusting()) return;

		int index = ((ListSelectionModel)evt.getSource()).getMinSelectionIndex();
		if(index >= 0){
			Packet p = (Packet)captor.getPackets().get(table.getSorter().getOriginalIndex(index));
			tree.analyzePacket(p);
			text.showPacket(p);
		}
	}

	public void loadProperty(){
		Component[] menus = new Component[analyzers.size()];

		JMenu[] tableViewMenu = captor.getFrame().getView();

		int k=0;
		for(int j=0; j<tableViewMenu[0].getMenuComponents().length; j++)
			menus[k++]=tableViewMenu[0].getMenuComponents()[j];
		for(int j=0; j<tableViewMenu[1].getMenuComponents().length; j++)
			menus[k++]=tableViewMenu[1].getMenuComponents()[j];
		for(int j=0; j<tableViewMenu[2].getMenuComponents().length; j++)
			menus[k++]=tableViewMenu[2].getMenuComponents()[j];
		for(int j=0; j<tableViewMenu[3].getMenuComponents().length; j++)
			menus[k++]=tableViewMenu[3].getMenuComponents()[j];

		StringTokenizer status=new StringTokenizer(Captor.preferences.get("TableView",
		"Ethernet Frame:Source MAC,Ethernet Frame:Destination MAC,IPv4:Source IP,IPv4:Destination IP"),",");

		while(status.hasMoreTokens()){
			StringTokenizer s = new StringTokenizer(status.nextToken(),":");
			if(s.countTokens() == 2){
				String name = s.nextToken();
				String valueName = s.nextToken();
				for(int i=0;i<menus.length;i++){
					if(((JMenu)menus[i]).getText()==null || name==null) 
						continue;
					if(((JMenu)menus[i]).getText().equals(name)){
						Component[] vn = ((JMenu)menus[i]).getMenuComponents();
						for(int j=0;j<vn.length;j++)
							if(valueName.equals(((JCheckBoxMenuItem)vn[j]).getText())){
								((JCheckBoxMenuItem)vn[j]).setState(true);
								break;
							}
						break;
					}
				}

				for(AbstractPacketAnalyzer analyzer:analyzers)
					if(analyzer.getProtocolName().equals(name)){
						table.setTableView(analyzer,valueName,true);
						break;
					}
			}
		}
	}

	public void saveProperty(){
		String[] viewStatus = table.getTableViewStatus();
		if(viewStatus.length>0){
			StringBuffer buf = new StringBuffer(viewStatus[0]);
			for(int i=1; i<viewStatus.length; i++)
				buf.append("," + viewStatus[i]);
			Captor.preferences.put("TableView",buf.toString());
		}
	}

	public TablePacket getTable() {
		return table;
	}

	public void setTable(TablePacket table) {
		this.table = table;
	}

	public void clearAll(){
		clearTable();
		clearTextArea();
		clearTree();
	}
}
